package top.lyc829.mymusic.ext

import android.graphics.Bitmap
import android.widget.ImageView
import androidx.core.graphics.drawable.RoundedBitmapDrawableFactory
import com.bumptech.glide.Glide
import com.bumptech.glide.load.MultiTransformation
import com.bumptech.glide.load.resource.bitmap.CenterCrop
import com.bumptech.glide.load.resource.bitmap.CircleCrop
import com.bumptech.glide.load.resource.bitmap.RoundedCorners
import com.bumptech.glide.request.RequestOptions
import com.bumptech.glide.request.target.BitmapImageViewTarget
import jp.wasabeef.glide.transformations.BlurTransformation
import jp.wasabeef.glide.transformations.RoundedCornersTransformation
import top.lyc829.mymusic.R

/**
 * @Author lyc
 * @create 2021-07-07 10:58
 */

fun ImageView.load(url: String, round: Int = 0, cornerType: RoundedCornersTransformation.CornerType = RoundedCornersTransformation.CornerType.ALL) {
    if (round == 0) {
        Glide.with(this.context).load(url).into(this)
    } else {
//        val option = RequestOptions.bitmapTransform(RoundedCornersTransformation(dp2px(round), 0, cornerType)).placeholder(
//            R.drawable.shape_album_loading_bg)
        val option = RequestOptions.bitmapTransform(MultiTransformation(CenterCrop(), RoundedCorners(round))).placeholder(
            R.drawable.shape_album_loading_bg
        )

        Glide.with(this.context).load(url).apply(option).into(this)
    }
}

fun ImageView.loadAsCircle(url: String) {
    Glide.with(this.context)
        .load(url)
        .apply(RequestOptions.bitmapTransform(CircleCrop()).placeholder(
            R.drawable.shape_album_loading_bg
        ))
        .into(this)
}

/**
 * 高斯模糊
 */
fun ImageView.loadWithBlur(url: String, radius: Int = 25) {
    Glide
        .with(this.context)
        .load(url)
        .apply(RequestOptions.bitmapTransform(BlurTransformation(radius, 3)))
        .placeholder(R.drawable.shape_album_loading_bg)
        .into(this)
}

/**
 * Glide加载图片，可以定义配置参数。
 *
 * @param url 图片地址
 * @param options 配置参数
 */
fun ImageView.load(url: String, options: RequestOptions.() -> RequestOptions) {
    Glide.with(this.context).load(url).apply(RequestOptions().options()).into(this)
}